Styling

For general information about styling parts of the chart control or the data series, see other topics including the various data series topics, Axis Display Options, Legend Display Options and Data Labelling Options.

The ChartingGallery sample includes lots of examples of styling charts which you can easily adapt to your own visual style.

Data-Dependent Styling

You can also create styles which adapt to the data being displayed: for example, displaying a different fill color depending on the range band a value falls into. To do this, you must specify a style for the data elements—the exact property will depend on the type of data series. For example, to apply data-dependent styling to a bar series, use the BarStyle property.

The key to data-dependent styling is to replace the control Template, and bind to the XObject and/or YObject property of the templated parent. (XObject and YObject are defined by the CardinalDataPoint class, from which classes such as Bar and ChartSymbol are derived.) In this example, we create a very simple flat rectangle, and use a value converter to specify the fill brush.

CopyStyling bars in a bar chart according to their value
<ms:BarSeries>

  <ms:BarSeries.Resources>
    <local:DataBrushConverter x:Key="DataBrush" />
  </ms:BarSeries.Resources>

  <ms:BarSeries.BarStyle>
    <Style TargetType="ms:Bar">
      <Setter Property="Template">
        <Setter.Value>
          <ControlTemplate>
            <Rectangle Fill="{Binding YObject, Converter={StaticResource DataBrush}, RelativeSource={RelativeSource TemplatedParent}}" />
          </ControlTemplate>
        </Setter.Value>
      </Setter>
    </Style>
  </ms:BarSeries.BarStyle>

</ms:BarSeries>
CopyValue converter for above style
public class DataBrushConverter : IValueConverter
{
  public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
  {
    return System.Convert.ToDouble(value) < 15000 ? new SolidColorBrush(Colors.Red) : new SolidColorBrush(Colors.Green);
  }

  public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
  {
    throw new NotImplementedException();
  }
}